package com.onadake.web.domain;

import java.util.Date;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;

import com.onadake.utils.DomainUtils;
import com.onadake.utils.ValidationUtils;

/**
 * Contains the profile information about user.
 * 
 * @author radek.hecl
 *
 */
public class UserProfile {

    /**
     * Builder object.
     */
    public static class Builder {

        /**
         * Email which is used as log on name.
         */
        private String email;

        /**
         * Kanji name of the user.
         */
        private String kanjiName;

        /**
         * Kana name of the user.
         */
        private String kanaName;

        /**
         * Date of birth.
         */
        private Date birthDate;

        /**
         * Sets email.
         * 
         * @param email email
         * @return this instance
         */
        public Builder setEmail(String email) {
            this.email = email;
            return this;
        }
        
        /**
         * Sets kanji name.
         * 
         * @param kanjiName kanji name
         * @return this instance
         */
        public Builder setKanjiName(String kanjiName) {
            this.kanjiName = kanjiName;
            return this;
        }
        
        /**
         * Sets kana name.
         * 
         * @param kanaName kana name
         * @return this instance
         */
        public Builder setKanaName(String kanaName) {
            this.kanaName = kanaName;
            return this;
        }
        
        /**
         * Sets date of birth.
         * 
         * @param birthDate date of birth
         * @return this instance
         */
        public Builder setBirthDate(Date birthDate) {
            this.birthDate = DomainUtils.copyDate(birthDate);
            return this;
        }
        
        /**
         * Builds the result object.
         * 
         * @return created object
         */
        public UserProfile build() {
            return new UserProfile(this);
        }
    }
    
    /**
     * Email which is used as log on name.
     */
    private String email;

    /**
     * Kanji name of the user.
     */
    private String kanjiName;

    /**
     * Kana name of the user.
     */
    private String kanaName;

    /**
     * Date of birth.
     */
    private Date birthDate;

    /**
     * Creates new instance.
     * 
     * @param builder builder object
     */
    public UserProfile(Builder builder) {
        email = builder.email;
        kanjiName = builder.kanjiName;
        kanaName = builder.kanaName;
        birthDate = DomainUtils.copyDate(builder.birthDate);
        guardInvariants();
    }
    
    /**
     * Guards this object to be consistent. Throws exception if this is not the case.
     */
    private void guardInvariants() {
        ValidationUtils.guardMatch(email, ".*@.*", "email must be defined");
        ValidationUtils.guardNotEmpty(kanjiName, "kanjiName cannot be empty");
        ValidationUtils.guardNotEmpty(kanjiName, "kanjiName cannot be empty");
        ValidationUtils.guardNotNull(birthDate, "birthDate cannot be null");
    }
    
    /**
     * Returns email of the user.
     * 
     * @return email email of the user
     */
    public String getEmail() {
        return email;
    }

    /**
     * Returns the kanji name.
     * 
     * @return kanjiName kanji name
     */
    public String getKanjiName() {
        return kanjiName;
    }

    /**
     * Returns the kana name.
     * 
     * @return kanaName kana name
     */
    public String getKanaName() {
        return kanaName;
    }

    /**
     * Returns date of birth.
     * 
     * @return birthDate date of birth
     */
    public Date getBirthDate() {
        return DomainUtils.copyDate(birthDate);
    }

    @Override
    public int hashCode() {
        return HashCodeBuilder.reflectionHashCode(this);
    }

    @Override
    public boolean equals(Object obj) {
        return EqualsBuilder.reflectionEquals(this, obj);
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

}
